package com.paginasenlanube.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Controller;


import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;





import com.google.gson.JsonArray;
import com.paginasenlanube.model.Archive;
import com.paginasenlanube.model.DataType;
import com.paginasenlanube.model.Page;
import com.paginasenlanube.model.SocialFeed;
import com.paginasenlanube.model.Website;


import com.paginasenlanube.service.PagesConfigService;

import com.paginasenlanube.utils.Utils;
import com.paginasenlanube.view.Config;


@Controller
@Scope("request")
public class BuildPageController {
	
	@Autowired
	 PagesConfigService pageService;
	
	
	public PagesConfigService getPageService() {
		return pageService;
	}
	
	@Autowired
	private Config cfg;
	
	
	@Autowired
	private Website website;
	
		 
	 @RequestMapping(value = "/ajax/Pagetemplate", method = RequestMethod.POST) 
	 public ModelAndView updatePageTemplate(@RequestParam("template") String template,@RequestParam("pageId") String pageId) {
		  Page p = pageService.updatePageTemplate(template,pageId);
		  ModelAndView mav = new ModelAndView();
		  mav.addObject("website",website);
	      mav.addObject("page",p);
	      mav.addObject("config",cfg);
	      mav.setViewName("paso2/pages/"+p.getPageType());
	      return mav;
	 }
	 
	
	 
	 @RequestMapping(value="ajax/page/load", method = RequestMethod.POST)
		public ModelAndView loadPage(@RequestParam("pageId") String page) {
		      Page p = pageService.getPage(page);
		      ModelAndView mav = new ModelAndView();
		      mav.addObject("website",website);
		      mav.addObject("page",p);
		      mav.addObject("config",cfg);
		      mav.setViewName("paso2/pages/"+p.getPageType());
		      return mav;
			
	 
		}
	 
	 @RequestMapping(value = "ajax/dataType", method = RequestMethod.POST)
	 public @ResponseBody String updateDataType(@ModelAttribute("dataType")
     						DataType dataType, BindingResult result) {
	         	        String returnText;
	        	        if(!result.hasErrors()){
	        	        	pageService.updateDataTypePage(dataType);
	        	            returnText = "Data has been added.";
	        	        }else{
	        	            returnText = "Sorry, an error has occur. Data has not been added.";
	        	        }
	        	        return returnText;
	    }
	 
	 @RequestMapping(value = "/ajax/addDataType", method = RequestMethod.POST) 
	 public ModelAndView  addDataType(@RequestParam("pageId") String pageId) {
		  Page p = pageService.addDataType(pageId);
		  ModelAndView mav = new ModelAndView();
		  mav.addObject("website",website);
	      mav.addObject("page",p);
	      mav.addObject("config",cfg);
	      mav.setViewName("paso2/pages/"+p.getPageType());
		 return mav;
	 }
	 
	 @RequestMapping(value = "/ajax/removeDataType", method = RequestMethod.POST) 
	 public ModelAndView  removeDataType(@RequestParam("pageId") String pageId,@RequestParam("dataTypeId") String dataTypeId) {
		  Page p = pageService.deleteDataType(pageId, dataTypeId);
		  ModelAndView mav = new ModelAndView();
		  mav.addObject("website",website);
	      mav.addObject("page",p);
	      mav.addObject("config",cfg);
	      mav.setViewName("paso2/pages/"+p.getPageType());
		 return mav;
	 }
	 
	 @RequestMapping(value = "ajax/updateArchive", method = RequestMethod.POST)
	 public @ResponseBody String addArchive(@ModelAttribute("archive")
     						Archive archive, BindingResult result) {
	         	        String returnText;
	        	        if(!result.hasErrors()){
	        	        	String json = pageService.updateArchivePage(archive);
	        	            returnText = json;
	        	        }else{
	        	            returnText = "Sorry, an error has occur. Data has not been added.";
	        	        }
	        	        return returnText;
	    }
	 
	 @RequestMapping(value = "/ajax/removeArchive", method = RequestMethod.POST) 
	 public ModelAndView  removeArchive(@RequestParam("pageId") String pageId,@RequestParam("archiveId") String archiveId) {
		  Page p = pageService.deleteArchive(pageId, archiveId);
		  ModelAndView mav = new ModelAndView();
		  mav.addObject("website",website);
	      mav.addObject("page",p);
	      mav.addObject("config",cfg);
	      mav.setViewName("paso2/pages/"+p.getPageType());
		 return mav;
	 }
	 
	  @RequestMapping(value = "ajax/updateArchivePosition", method = RequestMethod.POST)
	    public @ResponseBody String updateArchivePosition(@RequestParam("Data") String jsonString){
	    	JsonArray json = Utils.getJsonArray(jsonString);
	    	pageService.updateArchivePosition(json);
	    	return "success";
	    }
	  
	  @RequestMapping(value = "/ajax/loadArchive", method = RequestMethod.POST) 
		 public @ResponseBody String  loadArchive(@RequestParam("archiveId") String archiveId) {
			  String jsonArchive = pageService.getArchive(archiveId);
			  		   
			 return jsonArchive;
		 }
	  
	  @RequestMapping(value = "/ajax/addImageGallery", method = RequestMethod.POST) 
	  public @ResponseBody String addImageGallery(@RequestParam("pageId") String pageId,@RequestParam("name") String name,@RequestParam("description") String description) {
		  String jsonGalleries = pageService.addImageGallery(pageId,name,description);
		 return jsonGalleries;
	 }
	  
	  @RequestMapping(value = "/ajax/getImageGallery", method = RequestMethod.POST) 
	  public @ResponseBody String getImageGallery(@RequestParam("galleryId") String galleryId) {
		  String jsonGallery = pageService.getImageGallery(galleryId);
		 return jsonGallery;
	 }
	
}
